﻿using UnityEngine;
using System.Collections;

/// <summary>
/// 游戏数据核心
/// </summary>
public class GameDataKernel {

    static GameDataKernel _inst;
    public static GameDataKernel inst {
        get {
            if (_inst == null)
            {
                _inst = new GameDataKernel();
            }
            return _inst;
        }
    }

    ///////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// 状态
    /// </summary>
    public enum State
    {
        STATE_INIT = 1,
        STATE_CONNECTED = 2,
        STATE_WAITING = 3,
        STATE_GAMING = 4,
    }

    State _state;
    public State state { get { return _state; } set { _state = value; } }


    ///////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// 房间号
    /// </summary>
    int _roomId;
    public int roomId { get { return _roomId; } set { _roomId = value; } }

    ///////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// 服务器地址
    /// </summary>
    string _servAddr;
    public string servAddr { get { return _servAddr; } set { _servAddr = value; } }

    ///////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// 游戏玩家
    /// </summary>
    GamePlayer[] _players;
    public GamePlayer[] players { get { return _players; } }

    public const int PLAYER_ME = 0;
    public const int PLAYER_1 = 1;
    public const int PLAYER_2 = 2;

    ///////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// 游戏轮数
    /// </summary>
    int _round;
    /// <summary>
    /// 庄家
    /// </summary>
    int _dealer;
    /// <summary>
    /// 玩家位置
    /// </summary>
    int _playerIndex;
    /// <summary>
    /// 当前轮
    /// </summary>
    int _curRound;
    ///////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// 游戏界面
    /// </summary>
    UIGameWindow _gameWin;
    public UIGameWindow gameWin { set { _gameWin = value; } }
    ///////////////////////////////////////////////////////////////////////////

    /// <summary>
    /// 初始化玩家
    /// </summary>
    /// <param name="num"></param>
    /// <param name="playerCtrls"></param>
    public void InitPlayers(int num, UIGame_Player[] playerCtrls)
    {
        _players = new GamePlayer[num];

        _players[PLAYER_ME] = new GamePlayer();
        _players[PLAYER_ME].ui = playerCtrls[PLAYER_ME];
        _players[PLAYER_ME].UpdateBasic();

        _players[PLAYER_1] = new GamePlayer();
        _players[PLAYER_1].ui = playerCtrls[PLAYER_1];
        _players[PLAYER_1].UpdateBasic();

        _players[PLAYER_2] = new GamePlayer();
        _players[PLAYER_2].ui = playerCtrls[PLAYER_2];
        _players[PLAYER_2].UpdateBasic();
    }

    /// <summary>
    /// 处理消息
    /// </summary>
    /// <param name="message"></param>
    public void HandleMessage(IOMessage message)
    {
        DebugInfo.Log("GameDataKernel.HandleMessage: " + message.code);

        switch(message.code)
        {
            case IOCmds.SMSG_JOIN_ROOM:
                HandleJoinRoom(message.args);
                break;

            case IOCmds.SMSG_PLAYER_JOIN_ROOM:
                HandlePlayerJoinRoom(message.args);
                break;

            case IOCmds.SMSG_SET_ROOM_INFO:
                HandleSetRoomInfo(message.args);
                break;

            case IOCmds.SMSG_SET_PLAYER_OFFLINE:
                HandlePlayerOffline(message.args);
                break;

            case IOCmds.SMSG_SET_PLAYER_EXIT_ROOM:
                HandlePlayerExitRoom(message.args);
                break;
        }
    }

    /// <summary>
    /// 发送游戏消息
    /// </summary>
    /// <param name="message"></param>
    public void SendGameMessage(IOMessage message)
    {
        message.args["uid"] = DataKernel.inst.userInfo.uid;
        message.args["roomId"] = _roomId;
        NetIOController.inst.SendMessage(message);
    }

    /// <summary>
    /// 处理加入房间数据
    /// </summary>
    /// <param name="cArgs"></param>
    void HandleJoinRoom(HashtableEx cArgs)
    {
        _round = cArgs.GetInt("round");
        _dealer = cArgs.GetInt("dealer");
        _playerIndex = cArgs.GetInt("playerIndex");

        // 显示房间号
        _gameWin.SetRoomId(_roomId);

        HashtableEx players = cArgs.GetMapEx("players");
        if (players.ContainsKey("1"))
        {
            _InitPlayer(1, players.GetMapEx("1"));
        }
        if (players.ContainsKey("2"))
        {
            _InitPlayer(2, players.GetMapEx("2"));
        }
        if (players.ContainsKey("3"))
        {
            _InitPlayer(3, players.GetMapEx("3"));
        }
    }

    void HandlePlayerJoinRoom(HashtableEx cArgs)
    {
        _InitPlayer(cArgs.GetInt("playerIndex"), cArgs.GetMapEx("player"));
    }

    void HandlePlayerExitRoom(HashtableEx cArgs)
    {
        int playerIndex = cArgs.GetInt("playerIndex");
        _GetPlayer(playerIndex).info = null;
    }

    void HandleSetRoomInfo(HashtableEx cArgs)
    {
        _curRound = cArgs.GetInt("curRound");
        _dealer = cArgs.GetInt("dealer");

        //// 更新界面
        _gameWin.waitingCtrlVisible = false;

        // 更新庄标志
        GamePlayer player = _GetPlayer(_dealer);
        player.info.dealer = true;
        player.UpdateBasic();
    }

    void HandlePlayerOffline(HashtableEx cArgs)
    {
        int playerIndex = cArgs.GetInt("playerIndex");

        // 更新在线状态
        GamePlayer player = _GetPlayer(playerIndex);
        player.info.online = false;
        player.UpdateBasic();
    }

    /// <summary>
    /// 初始化玩家
    /// </summary>
    /// <param name="index"></param>
    /// <param name="initArgs"></param>
    void _InitPlayer(int index, HashtableEx initArgs)
    {
        GamePlayer curPlayer = _GetPlayer(index);
        curPlayer.info = new GamePlayerInfo(initArgs);
    }

    /// <summary>
    /// 根据索引获取位置
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    GamePlayer _GetPlayer(int index)
    {
        GamePlayer curPlayer = null;
        if (index == _playerIndex)
        {
            curPlayer = _players[PLAYER_ME];
        }
        else if ((index == (_playerIndex + 1)) || ((index + _players.Length) == (_playerIndex + 1)))
        {
            curPlayer = _players[PLAYER_2];
        }
        else
        {
            curPlayer = _players[PLAYER_1];
        }
        return curPlayer;
    }
}
